package com.chzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chzu.pojo.PaperInfo;
import com.chzu.pojo.QuestionInfo;
import com.chzu.pojo.StudentInfo;
import com.chzu.pojo.SysUser;
import com.chzu.pojo.vo.PaperAndAllQuestionVo;
import com.chzu.result.JsonResult;
import com.chzu.result.QueryInfo;
import com.chzu.service.PaperInfoService;
import com.chzu.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName PaperInfoController
 * @Description TODO
 * @Date 2022/02/22 11:04
 */
@RestController
@RequestMapping("/paperInfo")
@Api(tags = "试卷信息的接口")
public class PaperInfoController {

    @Resource
    private PaperInfoService paperInfoService;
    @Resource
    private StudentInfoService studentInfoService;

    @ApiOperation("查询所有试卷信息")
    @PostMapping("/questionInfoPage")
    public JsonResult<Object> getAllPaperInfo(@RequestBody QueryInfo queryInfo) {
        return paperInfoService.getAllPaperInfo(queryInfo);
    }

    @ApiOperation("修改某个试卷信息")
    @Transactional(rollbackFor= Exception.class)
    @PutMapping("/")
    public JsonResult<String> putPaperInfo(@RequestBody PaperInfo paperInfo) {
        QueryWrapper<PaperInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("paper_id",paperInfo.getPaperId());
        boolean result = paperInfoService.update(paperInfo,wrapper);
        if (!result) {
            return JsonResult.fail("修改试卷信息失败");
        }
        return JsonResult.ok("修改试卷信息成功");
    }

    @ApiOperation("删除某个试卷信息")
    @Transactional(rollbackFor= Exception.class)
    @DeleteMapping("/{paperId}")
    public JsonResult<String> deletePaperInfo(@PathVariable("paperId") String paperId) {
        QueryWrapper<PaperInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("paper_id",paperId);
        boolean result = paperInfoService.remove(wrapper);
        if (!result) {
            return JsonResult.fail("删除试卷信息失败");
        }
        return JsonResult.ok("删除试卷信息成功");
    }

    @ApiOperation("获取一张试卷中所有试题以及对应的选项")
    @GetMapping("/getAllQuestionsAndChose")
    public JsonResult<List<Map<String, Object>>> getAllQuestionsAndChose() {
        List<Map<String, Object>> data = paperInfoService.getAllQuestionsAndChose();
        return JsonResult.ok(data);
    }
}
